﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Data.OleDb
Public Class _Gallery
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'GridView2.Visible = False
    End Sub


    Protected Sub ListView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As ListViewSelectEventArgs) Handles ListView1.SelectedIndexChanging
        GridView2.Visible = True
        ListView1.SelectedIndex = e.NewSelectedIndex
        Dim Scat_ID = ListView1.SelectedDataKey.Value.ToString
        SqlDataSource4.SelectCommand = "SELECT [pic_id], [name], [imagelocation], [caption], [catID],[collection] FROM [gallery] WHERE ([catID] = " & Scat_ID & ")"
        GridView2.DataBind()
    End Sub

    Public Sub lnkdelete_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim lnkbtn As LinkButton = TryCast(sender, LinkButton)
        Dim gvrow As GridViewRow = TryCast(lnkbtn.NamingContainer, GridViewRow)
        Dim id As String = GridView2.DataKeys(gvrow.RowIndex).Value.ToString()
        Dim useN As String = User.Identity.Name

        Dim con As New OleDbConnection
        con = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        con.Open()

        Dim findNewTagIDCmd As New OleDbCommand("SELECT name FROM gallery WHERE pic_id=@f1", con)
        findNewTagIDCmd.Parameters.AddWithValue("@f1", id)
        Dim newTagDr = findNewTagIDCmd.ExecuteReader()
        newTagDr.Read()
        Dim id1 = newTagDr.Item("name")

        If useN <> "" And useN = id1 Then

            Dim cmd As New OleDbCommand
            cmd = New OleDbCommand("DELETE FROM gallery WHERE pic_id=@f1 and name=@f2", con)
            cmd.Parameters.AddWithValue("@f1", id)
            cmd.Parameters.AddWithValue("@f2", id1)
            cmd.ExecuteNonQuery()

            cmd.Dispose()
            con.Close()
            Response.Redirect("gallery.aspx")
        End If

    End Sub
End Class